package com.ruoyi.publish.service.impl;

import java.util.List;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtilTool;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.publish.domain.IdAcceptRegister;
import com.ruoyi.publish.mapper.IdAcceptRegisterMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.publish.mapper.IdIdentsampleMapper;
import com.ruoyi.publish.domain.IdIdentsample;
import com.ruoyi.publish.service.IIdIdentsampleService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

import static com.ruoyi.common.utils.StringUtilTool.numberSupplement;

/**
 * 鉴定样本Service业务层处理
 *
 * @author 张泽望
 * @date 2020-11-08
 */
@Service
public class IdIdentsampleServiceImpl implements IIdIdentsampleService {

    private static final Logger log = LoggerFactory.getLogger(IdIdentsampleServiceImpl.class);


    @Autowired
    private IdIdentsampleMapper idIdentsampleMapper;

    @Autowired
    private IdAcceptRegisterMapper idAcceptRegisterMapper;

    /**
     * 查询鉴定样本
     *
     * @param id 鉴定样本ID
     * @return 鉴定样本
     */
    @Override
    public IdIdentsample selectIdIdentsampleById(Long id) {
        return idIdentsampleMapper.selectIdIdentsampleById(id);
    }

    @Override
    public List<IdIdentsample> selectIdIdentsampleByIds(Long[] ids) {
        return idIdentsampleMapper.selectIdIdentsampleByIds(ids);
    }

    /**
     * 查询鉴定样本列表
     *
     * @param idIdentsample 鉴定样本
     * @return 鉴定样本
     */
    @Override
    public List<IdIdentsample> selectIdIdentsampleList(IdIdentsample idIdentsample) {
        return idIdentsampleMapper.selectIdIdentsampleList(idIdentsample);
    }

    /**
     * 新增鉴定样本
     *
     * @param idIdentsample 鉴定样本
     * @return 结果
     */
    @Override
    public int insertIdIdentsample(IdIdentsample idIdentsample) {
        idIdentsample.setCreateTime(DateUtils.getNowDate());

        //设置样本初始审核状态为未鉴定
        idIdentsample.setStatus("0");
        IdAcceptRegister idAcceptRegister = idAcceptRegisterMapper.selectIdAcceptRegisterById(idIdentsample.getRegisterId());
        if (idAcceptRegister != null) {
            String regesterCode=idAcceptRegister.getEx1();
            System.out.println("getSampleNum="+idAcceptRegister.getSampleNum());

            String sampleCode=regesterCode+"-"+ StringUtilTool.numberSupplement(String.valueOf(idAcceptRegister.getSampleNum()+1),4);
            idIdentsample.setSampleId(sampleCode);
            idIdentsample.setSampleCode(sampleCode);

            System.out.println("123"+idIdentsample.getSampleCode());
        }
        // 启用状态 默认为 1 启用
        if (StringUtils.isEmpty(idIdentsample.getIsUse())) {
            idIdentsample.setIsUse("1");
        }

         int insertNum= idIdentsampleMapper.insertIdIdentsample(idIdentsample);
         if (insertNum>0){
             idAcceptRegister.setSampleNum(idAcceptRegister.getSampleNum()+1);
             idAcceptRegisterMapper.updateIdAcceptRegister(idAcceptRegister);
         }
        return insertNum;
    }

    /**
     * 修改鉴定样本
     *
     * @param idIdentsample 鉴定样本
     * @return 结果
     */
    @Override
    public int updateIdIdentsample(IdIdentsample idIdentsample) {
        idIdentsample.setUpdateTime(DateUtils.getNowDate());
        return idIdentsampleMapper.updateIdIdentsample(idIdentsample);
    }

    /**
     * 删除鉴定样本对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteIdIdentsampleByIds(String ids) {
        return idIdentsampleMapper.deleteIdIdentsampleByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除鉴定样本信息
     *
     * @param id 鉴定样本ID
     * @return 结果
     */
    @Override
    public int deleteIdIdentsampleById(Long id) {
        return idIdentsampleMapper.deleteIdIdentsampleById(id);
    }

    /**
     * 改变记录启用状态
     *
     * @param idIdentsample 鉴定样本ID
     * @return 结果
     */
    @Override
    public int changeStatus(IdIdentsample idIdentsample) {
        IdIdentsample newSample = idIdentsampleMapper.selectIdIdentsampleById(idIdentsample.getId());
        System.out.println("id=" + newSample.getId());
        if (newSample != null && !idIdentsample.getIsUse().equals(newSample.getIsUse())) {
            newSample.setIsUse(idIdentsample.getIsUse());
            return idIdentsampleMapper.updateIdIdentsample(idIdentsample);
        } else {
            return 0;
        }
    }

    @Override
    public List<IdIdentsample> selectIdIdentsampleRegisterList(IdIdentsample idIdentsample) {
        return idIdentsampleMapper.selectIdIdentsampleRegisterList(idIdentsample);

    }

    @Override
    public String getSampleCode(IdIdentsample idIdentsample) {

        long registerId = idIdentsample.getRegisterId();
        String sampleType = idIdentsample.getSampleType();
        IdAcceptRegister idAcceptRegister = idAcceptRegisterMapper.selectIdAcceptRegisterById(idIdentsample.getRegisterId());
        String sampleTypelist = idAcceptRegister.getCategory();

        return idIdentsampleMapper.getSampleCode(registerId, sampleType, sampleTypelist);
    }

    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public String importIdIdentsample(List<IdIdentsample> idIdentsampleList, Boolean isUpdateSupport, String operName,String userName, String year, long registerId) {
        if (StringUtils.isNull(idIdentsampleList) || idIdentsampleList.size() == 0) {
            throw new BusinessException("导入案件数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        int orderNum = 0;
        for (IdIdentsample entity : idIdentsampleList) {
            orderNum++;
            if (StringUtils.isEmpty(entity.getSampleName())||StringUtils.isEmpty(entity.getSampleType())){
                continue;
            }
            try {

                //保存记录
                entity.setCreateBy(operName);
                entity.setTakeoverUser(userName);

                entity.setIsUse("1");//启用
                entity.setRegisterId(registerId);

                int u = insertIdIdentsample(entity);


                // 验证是否保成功
                if (u == 1) {

                    successNum++;
//                    successMsg.append("<br/>" + successNum + "、账号 " + user.getLoginName() + " 导入成功");
                } else {
                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、账号 " + user.getLoginName() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + " 导入失败：";
                // failureMsg.append(e.getMessage());
                log.error(msg, e);
            }
            orderNum++;
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
        }
        return successMsg.toString();
    }


    /**
     * 根据id导出数据
     *
     * @param ids
     * @return
     */
    @Override
    public List<IdIdentsample> selectRecordByIds(String[] ids) {
        return idIdentsampleMapper.selectRecordByIds(ids);
    }

    @Override
    public int idSampleToNull(Long id) {
        return idIdentsampleMapper.idSampleToNull(id);
    }


}
